﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 岗位管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20 
    /// </summary>
    public class PositionBll
    {
        private PositionIService service = new PositionService();
        private static List<PositionEntity> posList = new List<PositionEntity>();
        public PositionBll()
        {
            if (CacheFactory.Cache.Exists(CacheKey.POSITIONLIST))
            {
                posList = CacheFactory.Cache.Get<List<PositionEntity>>(CacheKey.POSITIONLIST);
            }
            else
            {
                if (UserProvider.Instance.Get() != null)
                {
                    posList = service.GetList().Result;
                    CacheFactory.Cache.Insert(CacheKey.POSITIONLIST, posList);
                }
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<PositionEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<PositionEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<List<PositionEntity>> GetListByUserId(string userId)
        {
            try
            {
                return await service.GetListByUserId(userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<PositionEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 重复验证（名称）
        /// </summary>
        /// <param name="fullName">名称</param>
        /// <param name="id">键值</param>
        /// <returns></returns>
        public async Task<bool> IsExistByFullName(string fullName, string id)
        {
            try
            {
                var condition = ExtLinq.True<PositionEntity>().And(m => m.FullName == fullName);
                if (!string.IsNullOrEmpty(id))
                    condition = condition.And(m => m.Id != id);
                return await service.IsExist(condition);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 重复验证（编码）
        /// </summary>
        /// <param name="enCode">编码</param>
        /// <param name="id">键值</param>
        /// <returns></returns>
        public async Task<bool> IsExistByEnCode(string enCode, string id)
        {
            try
            {
                var condition = ExtLinq.True<PositionEntity>().And(m => m.EnCode == enCode);
                if (!string.IsNullOrEmpty(id))
                    condition = condition.And(m => m.Id != id);
                return await service.IsExist(condition);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Delete(PositionEntity entity)
        {
            try
            {
                await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Create(PositionEntity entity)
        {
            try
            {
                CacheFactory.Cache.Remove(CacheKey.POSITIONLIST);
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        public async Task<int> Update(string id, PositionEntity entity)
        {
            try
            {
                CacheFactory.Cache.Remove(CacheKey.POSITIONLIST);
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取名字
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public string GetName(string ids)
        {
            if (ids.IsNullOrEmpty())
            {
                return "";
            }
            var idList = new List<string>(ids.Split(","));
            var nameList = new List<string>();
            foreach (var item in idList)
            {
                var info = posList.Find(x => x.Id == item);
                if (info != null && !info.FullName.IsEmpty())
                {
                    nameList.Add(info.FullName);
                }
            }
            var name = string.Join(",", nameList);
            return name;
        }
    }
}
